package cn.rkylin.oms.common.event;

import java.util.List;
import java.util.Map;

/**
 * 事件管理器
 * 事件上下文
 * 
 * @author wangxiaoyi
 * @since: 2017年4月13日 3:44
 */
@SuppressWarnings("rawtypes")
public class EventBusManager {
	private IEventBus eventBus;
	private Map<String, List<EventHandlerConfig>> handlers;

	public void fireEvent(BusinessEvent event) throws Exception {
		eventBus.fireEvent(event);
	}

	public IEventBus getEventBus() {
		return eventBus;
	}

	public void setEventBus(IEventBus eventBus) {
		this.eventBus = eventBus;
	}

	public Map<String, List<EventHandlerConfig>> getHandlers() {
		return handlers;
	}

	public void setHandlers(Map<String, List<EventHandlerConfig>> handlers) {
		this.handlers = handlers;
		for (Map.Entry<String, List<EventHandlerConfig>> cfg : handlers.entrySet()) {
			String typeCode = cfg.getKey();
			for (EventHandlerConfig config : cfg.getValue()) {
				EventHandler handler = config.getHandler();
				handler.setPosition(config.getPosition());
				eventBus.addHandler(EventTypeCodeEnum.fromCode(typeCode), handler, config.isDefer());
			}
		}
	}

}
